package com.atguigu.orderservice.controller;


import com.atguigu.common.R;
import com.atguigu.orderservice.service.TPayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-04-24
 */
@RestController
@RequestMapping("/orderservice/paylog")
//@CrossOrigin
public class TPayLogController {

    @Autowired
    private TPayLogService payLogService;

    //生成支付二维码接口
    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        //返回map集合，包含支付二维码地址，订单号等等
        Map map = payLogService.createNative(orderNo);

        return R.ok().data(map);
    }

    //查询订单支付状态
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        //调用微信接口查询
        Map map = payLogService.queryPayStatus(orderNo);
        System.out.println("微信返回支付状态信息:"+map);
        if(map == null) {
            return R.error().message("支付失败");
        }
        //从微信接口返回map里面判断是否成功
        if(map.get("trade_state").equals("SUCCESS")) {//成功
            //更新订单表数据，添加支付记录
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }
}

